package com.platform.modules.wallet.take;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.config.PlatformConfig;
import com.platform.common.enums.YesOrNoEnum;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.domain.WalletAccount;
import com.platform.modules.wallet.domain.WalletDailyBill;
import com.platform.modules.wallet.domain.WalletRedPacketSub;
import com.platform.modules.wallet.domain.WalletTransfer;
import com.platform.modules.wallet.service.WalletAlarmService;
import com.platform.modules.wallet.service.WalletDailyBillhService;
import com.platform.modules.wallet.service.WalletRedPacketSubService;
import com.platform.modules.wallet.service.WalletTransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class DailyBillTask {

    @Resource
    private WalletDailyBillhService walletDailyBillhService;
    @Resource
    private WalletAlarmService alarmService;

    /**
     * 定时生成账单
     */
    @Scheduled(cron = "0 0 0 * * ?")//"0 5/10 9-23 * * ?"
    public void runCreateBill() throws InterruptedException {

        if (PlatformConfig.MASTER.equals(YesOrNoEnum.NO)) return;
        walletDailyBillhService.createDailyBill();

        log.info("定时生成账单 时间：{}", DateUtil.date());
    }

    /**
     * 定时校验账单
     */
    @Scheduled(cron = "30 */5 * * * ?")//"0 5/10 9-23 * * ?"
    public void runVerifyBill() throws InterruptedException {

        if (PlatformConfig.MASTER.equals(YesOrNoEnum.NO)) return;

        log.info("扫描账单开始");
        Date date1 = DateUtil.date();
        long pages = 10;

        for (int i = 1; i < pages; i++) {

            IPage<WalletDailyBill> iPage = walletDailyBillhService.queryPage(i, 1000);
            pages = iPage.getPages();
            List<WalletDailyBill> records = iPage.getRecords();

            for (WalletDailyBill record : records) {
                if (!StrUtil.equals(record.getPrivateKey(), record.getKey())) {
                    String contentTemp = "账单Bill加密校验失败,id:{} userId：{}";
                    String format = StrUtil.format(contentTemp, record.getId(), record.getUserId());
                    alarmService.addAlarm(format);
                    MailUtil.send("61990988@qq.com", "系统发出告警,账单Bill加密校验失败", format, false);
                }
            }
            log.info("总页：{} 页码：{}", iPage.getPages(), iPage.getCurrent());
        }

        log.info("扫描账单结束");
        Date date2 = DateUtil.date();

        long betweenMinute = DateUtil.between(date1, date2, DateUnit.SECOND);
        log.info("扫描账单用时：{}秒", betweenMinute);

        log.info("定时扫描账单 时间：{}", DateUtil.date());
    }

//    @PostConstruct
//    public void init() {
//        System.out.println("手动结算");
//        walletDailyBillhService.createDailyBill();
//    }
}
